#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <set>
using namespace std;

void test_set1()
{
	unordered_set<int> s = { 3,1,6,7,8,2 };
	/*unordered_set<int>::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;*/

	srand(time(0));
	for (size_t i = 0; i < 22000; ++i)
	{
		s.insert(rand()); // N比较大时，重复值比较多
		//v.push_back(rand()+i); // 重复值相对少
		//v.push_back(i); // 没有重复，有序
	}

	/*for (auto e : s)
	{
		cout << e << " ";
	}
	cout << endl;*/

	cout << s.bucket_count() << endl;
	//cout << s.max_bucket_count() << endl;
	cout << s.size() << endl;
	cout << "负载因子：" << s.load_factor() << endl;
	cout << "最大负载因子：" << s.max_load_factor() << endl;

	size_t len = 0;
	size_t nonEmptyBucketSize = 0;
	size_t maxLen = 0;
	for (size_t i = 0; i < s.bucket_count(); i++)
	{
		if (s.bucket_size(i) > 0)
		{
			if (s.bucket_size(i) > maxLen)
				maxLen = s.bucket_size(i);

			len += s.bucket_size(i);
			++nonEmptyBucketSize;
		}
	}
	cout << "平均每个桶的长度:" << (double)len / nonEmptyBucketSize << endl;
	cout << "最大的桶的长度:" << maxLen << endl;
}

int test_set2()
{
	const size_t N = 1000000;

	unordered_set<int> us;
	set<int> s;

	vector<int> v;
	v.reserve(N);
	srand(time(0));
	for (size_t i = 0; i < N; ++i)
	{
		//v.push_back(rand()); // N比较大时，重复值比较多
		v.push_back(rand() + i); // 重复值相对少
		//v.push_back(i); // 没有重复，有序
	}

	// 21:15
	size_t begin1 = clock();
	for (auto e : v)
	{
		s.insert(e);
	}
	size_t end1 = clock();
	cout << "set insert:" << end1 - begin1 << endl;

	size_t begin2 = clock();
	us.reserve(N);
	for (auto e : v)
	{
		us.insert(e);
	}
	size_t end2 = clock();
	cout << "unordered_set insert:" << end2 - begin2 << endl;

	int m1 = 0;
	size_t begin3 = clock();
	for (auto e : v)
	{
		auto ret = s.find(e);
		if (ret != s.end())
		{
			++m1;
		}
	}
	size_t end3 = clock();
	cout << "set find:" << end3 - begin3 << "->" << m1 << endl;

	int m2 = 0;
	size_t begin4 = clock();
	for (auto e : v)
	{
		auto ret = us.find(e);
		if (ret != us.end())
		{
			++m2;
		}
	}
	size_t end4 = clock();
	cout << "unorered_set find:" << end4 - begin4 << "->" << m2 << endl;

	cout << "插入数据个数：" << s.size() << endl;
	cout << "插入数据个数：" << us.size() << endl << endl;

	size_t begin5 = clock();
	for (auto e : v)
	{
		s.erase(e);
	}
	size_t end5 = clock();
	cout << "set erase:" << end5 - begin5 << endl;

	size_t begin6 = clock();
	for (auto e : v)
	{
		us.erase(e);
	}
	size_t end6 = clock();
	cout << "unordered_set erase:" << end6 - begin6 << endl << endl;

	return 0;
}



void test_map1()
{
	unordered_map<string, string>dict;
	dict.insert({ "left","左边" });
	dict["sort"];
	dict["sort"] = "排序";

}

#include "HashTable.h"

//int main()
//{
//	//test_set1();
//	//test_map1();
//
//	//open_address::TestHT1();
//	//open_address::TestHT2();
//	//cout << endl;
//	//hash_bucket::TestHT1();
//	//hash_bucket::TestHT2();
//
//
//	return 0;
//}



#include "UnorderedMap.h"
#include "UnorderedSet.h"

int main()
{
	test_set1();
	//bit::test_set();
	//bit::test_map();
}